<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "loan".
 *
 * @property integer $id
 * @property integer $country_id
 * @property integer $user_id
 * @property double $num
 * @property integer $cycle
 * @property double $rate
 * @property double $interest
 * @property string $apply
 * @property integer $status
 * @property string $apply_time
 * @property string $check_time
 * @property string $create_at
 * @property string $update_at
 *
 * @property Country $country
 * @property User $user
 */
class Loan extends \app\models\BaseModel {
    const STATUS_CREATE = 1;
    const STATUS_CANCEL = 2;
    const STATUS_AUTH = 3;
    const STATUS_BACK = 4;
    const STATUS_REJECT = 5;
    public static $positions = [
        self::STATUS_CREATE => '待审核',
        self::STATUS_CANCEL => '已取消',
        self::STATUS_AUTH => '已通过',
        self::STATUS_BACK => '已偿还',
        self::STATUS_REJECT => '已拒绝',
    ];

    /**
     * @inheritdoc
     */
    public static function tableName() {
        return 'loan';
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['country_id', 'user_id', 'num', 'cycle', 'rate', 'interest', 'apply', 'status', 'apply_time', ], 'required'],
            [['country_id', 'user_id', 'cycle', 'status'], 'integer'],
            [['num', 'rate', 'interest'], 'number'],
            [['apply_time', 'check_time', 'create_at', 'update_at'], 'safe'],
            [['apply'], 'string', 'max' => 255],
            [['country_id'], 'exist', 'skipOnError' => true, 'targetClass' => Country::className(), 'targetAttribute' => ['country_id' => 'id']],
            [['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['user_id' => 'id']],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'country_id' => 'Country ID',
            'user_id' => 'User ID',
            'num' => '贷款金额',
            'cycle' => '贷款周期',
            'rate' => '贷款利率',
            'interest' => '到期利息',
            'apply' => '资金用途',
            'status' => '状态',
            'apply_time' => '提交事件',
            'check_time' => '审核时间',
            'create_at' => '创建时间',
            'update_at' => '更新时间',
        ];
    }

    public function fields() {
        $fields = [
            'id',
            'country',
            'user',
            'num',
            'cycle',
            'rate',
            'interest',
            'apply',
            'status',
            'apply_time',
            'check_time',
        ];
        $fields = array_combine($fields, $fields);

        return $fields;
    }

    public function extraFields() {
        $fields = [
            'country_id',
            'user_id',
            'create_at',
            'update_at',
        ];
        $fields = array_combine($fields, $fields);
        return $fields;
    }

    public function getCountry() {
        return $this->hasOne(Country::className(), ['id' => 'country_id'])->inverseOf('loans');
    }

    public function getUser() {
        return $this->hasOne(User::className(), ['id' => 'user_id'])->inverseOf('loans');
    }
}
